package com.itbaima.controller;

import com.itbaima.pojo.entity.Department;
import com.itbaima.pojo.entity.HrSalary;
import com.itbaima.pojo.entity.Salary;
import com.itbaima.result.Result;
import com.itbaima.service.DepartmentService;
import com.itbaima.service.SalaryStandardService;
import com.itbaima.service.UserService;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/department")  // 统一前缀
@Slf4j
public class SalaryStandardController {

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private UserService userService;  // 注入 UserService

    @Autowired
    private SalaryStandardService salaryStandardService; // 注入 SalaryStandardService

    // 获取薪酬标准和部门信息
    @PostMapping("/getSalaryId")
    public Map<String, Object> getDepartmentAndSalaryId(@RequestBody Map<String, String> request) {
        Map<String, Object> response = new HashMap<>();
        String departmentName = request.get("department_name");

        try {
            // 调用 service 层方法
            Department department = departmentService.getDepartmentAndSalaryId(departmentName);

            if (department != null) {
                // 计算薪酬标准ID
                Integer salaryId = department.getDepartmentId() - 3;
                // 返回计算后的薪酬标准ID和部门ID
                Map<String, Object> departmentData = new HashMap<>();
                departmentData.put("department_id", department.getDepartmentId());
                departmentData.put("salary_id", salaryId);

                response.put("code", 1);
                response.put("data", departmentData);
            } else {
                response.put("code", 0);
                response.put("msg", "未找到对应部门");
            }
        } catch (Exception e) {
            response.put("code", 0);
            response.put("msg", "查询失败");
            e.printStackTrace();
        }

        return response;
    }

    // 获取当前登录用户姓名
    @GetMapping("/getCurrentUserName")
    public Result getCurrentUserName(HttpSession session) {
        // 输出调试信息，查看当前会话中的所有属性
        System.out.println("【调试信息】请求到达后端：获取当前用户名");

        // 尝试从 session 中获取用户名
        String name = (String) session.getAttribute("name");

        // 如果找不到用户，打印调试信息并返回错误
        if (name == null) {
            System.out.println("【调试信息】未找到当前用户：session 中的 'name' 属性为空");
            return Result.error("未找到当前用户");
        }

        // 如果成功找到用户，打印调试信息
        System.out.println("【调试信息】当前用户：" + name);

        // 返回成功的响应，携带用户名
        return Result.success(name);
    }

    // 提交薪酬标准并保存到数据库
    @PostMapping("/submit")
    public Result submitSalaryStandard(@RequestBody Salary salary, HttpSession session) {
        try {
            System.out.println("运行了");
            // 获取当前登录用户姓名并填充到薪酬标准对象的登记人字段
            String registrar = (String) session.getAttribute("name");
            if (registrar == null) {
                return Result.error("未找到当前用户");
            }
            salary.setRegistrar(registrar);  // 自动填充登记人
            salary.setRegistrar_id(123);  // 假设这里需要从 session 获取用户 ID，当前设为示例 ID
            salary.setRegistration_time(java.time.LocalDateTime.now());  // 设置当前时间

            // 将前端的部门名称赋值给薪酬标准名称
            salary.setSalary_standard_name(salary.getSalary_standard_name());  // departmentName 映射到 salaryStandardName
            System.out.println(salary);
            // 调用服务层方法进行插入操作
            salaryStandardService.insertSalaryStandard(salary);

            return Result.success("薪酬标准提交成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("薪酬标准提交失败");
        }
    }


    @PostMapping("/abcd")
    public Result submitsalaryStd(@RequestBody List<HrSalary> salary, ServletRequest servletRequest){

        log.info("正在测试");

salaryStandardService.submit2(salary) ;

        return Result.success();
    }



}
